package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VueResult;
import m.html.VueFormResult;
import m.html.form.FormButton;
import m.html.form.FormEvent;
import m.html.form.FormInput;
import m.html.form.ModelForm;
import m.system.util.JSONMessage;
import manage.model.ServerSetting;
import manage.service.ServerSettingService;

@ActionMeta(name="manage/serverSetting")
public class ServerSettingAction extends ManageAction {

	private ServerSetting model;
	
	public JSONMessage doSave() throws Exception {
		systemLog("服务设置","保存服务设置");
		verifyAdminOperPower("manage_system_power");
		String msg=getService(ServerSettingService.class).save(model);
		return new JSONMessage(0,msg);
	}
	public JSONMessage doDeleteAll() throws Exception {
		verifyAdminOperPower("manage_system_power");
		getService(ServerSettingService.class).deleteAllRecord();
		return new JSONMessage(0,"删除成功");
	}

	public VueResult toEdit() throws Exception {
		ModelForm form = new ModelForm("监控设置",2);
		form.addItem(
			FormInput.radio("请求监控", "model.requestMonitor", "manage.model.ServerSetting!requestMonitor").setSpan(2),
			FormInput.radio("记录时长", "model.recordDuration", "manage.model.ServerSetting!recordDuration").setSpan(2),
			FormInput.radio("保存时长", "model.saveDuration", "manage.model.ServerSetting!saveDuration").setSpan(2),
			FormInput.radio("保存错误SQL", "model.saveErrorSql", "manage.model.ServerSetting!saveErrorSql"),
			FormInput.integer("慢SQL超时", "model.slowSqlSecond", "秒")
		)
		.addData("model",getService(ServerSettingService.class).getUniqueModel())
		.addButton(
			FormButton.pri("保存","doSave")
		)
		.addMethod(
			FormEvent.backFlag,
			FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
				.success("backFlag")
		);
		VueFormResult result=new VueFormResult();
		return result.addForm(form);
	}

	public ServerSetting getModel() {
		return model;
	}

	public void setModel(ServerSetting model) {
		this.model = model;
	}
}
